package org.thread.test05.tx;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * @author ZQX
 * @date 2025-03-14
 */
@WebServlet("/online")
public class Hello extends HttpServlet {

    // 注意：这里可能会发生线程安全问题 - 避免在 Servlet 中定义成员变量
    private int i = 0 ;


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        synchronized (Object.class) {
            resp.getWriter().println("在线人数：" + i);
            i++;
        }

    }
}
